#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2021/6/22 11:43
# @Author   : chengfeng
# @Email    : chengfeng@inhand.com.cn
# @File     : Config.py

from configparser import ConfigParser

import os


class Config:
    # titles:
    TITLE_DEBUG = "test_info"

    # values:
    # [test_info]
    VALUE_HOST = 'host'
    VALUE_PROTOCOL = 'protocol'
    VALUE_USERNAME = 'username'
    VALUE_PASSWORD = 'password'
    VALUE_BROWSER = 'browser'

    #path
    path_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))

    def __init__(self):
        """
        初始化
        """
        self.config = ConfigParser()
        self.conf_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.ini')
        self.allure_report_path = Config.path_dir + '/Report/allure-results'
        if not os.path.exists(self.conf_path):
            raise FileNotFoundError('请确保配置文件存在！')

        self.config.read(self.conf_path, encoding='gbk')

        # 获取配置文件信息
        self.host = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_HOST)
        self.protocol = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_PROTOCOL)
        self.username = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_USERNAME)
        self.password = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_PASSWORD)
        self.browser = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_BROWSER)

    def get_conf(self, title, value):
        """
        配置文件读取
        :param title:
        :param value:
        :return:
        """
        return self.config.get(title, value)

    def set_conf(self, title, value, text):
        """
        配置文件修改
        :param title:
        :param value:
        :param text:
        :return:
        """
        self.config.set(title, value, text)
        with open(self.conf_path, 'w+', encoding='utf-8') as f:
            return self.config.write(f)

    def add_conf(self, title):
        """
        配置文件添加
        :param title:
        :return:
        """
        self.config.add_section(title)
        with open(self.conf_path, "w+", encoding='utf-8') as f:
            return self.config.write(f)


if __name__ == "__main__":
    conf = Config()
    print(conf.host)